// 依赖 plus 对象，需在mui.plusReady()中调用
import axios from '../ajax/ajax'
/*
         上传图片的类型 upload_img_type
       *     square-->社区广场 上传图片
       *     user-->用户头像 上传图片
       *     goods-->商品评论 上传图片
       *
        this.$store.commit("change_upload_img_type","square")
*
使用此组件的时候，先在 created 里面初始化后一下 上传图片的类型，本项目暂时 就只有这三处地方需要上传图片
* */
var ts = {
//	upload_url: 'http://111.231.207.210:2999/upload',//API接口
//	img_head:'http://111.231.207.210:2999/',//静态目录
  upload_url: 'https://zhapp.4hl.cn/api/system/comment_uploads',//API接口
  img_head: 'https://zhapp.4hl.cn/',
  select_img: function(el, cb) {

    document.querySelector("#" + el).addEventListener("click", function() {
      plus.gallery.pick(function(img_path) {
//				console.log("选择照片完成")
        ts.uploadHead(img_path, function(path) {
          cb(path)
        });
      }, function(err) {
        console.log('err')
      });
    })
  },
  get_img: function(el, cb) {

    document.querySelector("#" + el).addEventListener("click", function() {
      // 拍照
      var cmr = plus.camera.getCamera();
      var res = cmr.supportedImageResolutions[0];
      var fmt = cmr.supportedImageFormats[0];
      cmr.captureImage(function(path) {
//				console.log("拍照成功: " + path);
        plus.io.resolveLocalFileSystemURL(path, function(path2) {
            var new_path = path2.toLocalURL();
            ts.uploadHead(new_path, function(img_path) {
              cb(img_path)
            })
          },
          function(error) {
            alert("拍照失败: " + error.message);
          }, {
            resolution: res,
            format: fmt
          }
        );
      })
    })
  },
  uploadHead: function(imgPath, callback) {
    //console.log("进入上传图片方法,接收到图片路径为" + imgPath)
    var image = new Image();
    image.src = imgPath;
    var ex = imgPath.slice(-3)
    //console.log(ex)
    image.onload = function() {
      var imgData = ts.getBase64Image(image);
//			console.log(imgData);
//			console.log("图片base64长度：" + imgData.length);

      /*
      axios.post("api/system/comment_uploads",{
        imgData: imgData
      }).then(res=>{
        if(res){
          callback(ts.img_head + res)
        }

        console.log(res);
      }).catch(err=>{
        console.log(JSON.stringify(err))
        mui.toast("图片上传失败")
      })
      */
      document.getElementById("loading_box").style.display="block"
      mui.post(ts.upload_url, {
        imgData: imgData
      }, function(data) {
        document.getElementById("loading_box").style.display="none"
        callback(ts.img_head + data)


      });


    }
  },
  getBase64Image: function(img) {
    var canvas = document.createElement("canvas");
    var width = img.width;
    //console.log(width);
    var height = img.height;
    //console.log(height);

    // calculate the width and height, constraining the proportions
    if(width > height) {
      if(width > 480) {
        height = Math.round(height *= 480 / width);

        width = 480;
      }
    } else {
      if(height > 480) {
        width = Math.round(width *= 480 / height);
        height = 480;
      }
    }
    canvas.width = width; /*设置新的图片的宽度*/

    canvas.height = height; /*设置新的图片的长度*/

    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0, width, height); /*绘图*/
    var dataURL = canvas.toDataURL("image/jpeg", 0.8);
    //	console.log(dataURL)
    return dataURL.replace("data:image/jpeg;base64,", "");
  }
}


export default ts;
